package com.ruoyi.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.PhgRequirement;
import com.ruoyi.mes.mapper.PhgRequirementMapper;
import com.ruoyi.mes.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.PhgRmOrderMapper;
import com.ruoyi.mes.domain.PhgRmOrder;
import com.ruoyi.mes.service.IPhgRmOrderService;

/**
 * 采购订单Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class PhgRmOrderServiceImpl implements IPhgRmOrderService 
{
    @Autowired
    private PhgRmOrderMapper phgRmOrderMapper;

    @Autowired
    private PhgRequirementMapper phgRequirementMapper;

    /**
     * 查询采购订单
     * 
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public PhgRmOrder selectPhgRmOrderById(Long id)
    {
        return phgRmOrderMapper.selectPhgRmOrderById(id);
    }

    /**
     * 根据产品id查询采购订单
     *
     * @param proId 采购订单主键
     * @return 采购订单
     */
    @Override
    public List<PhgRmOrder> selectPhgRmOrderByproductId(Long proId)
    {
        return phgRmOrderMapper.selectPhgRmOrderByproductId(proId);
    }

    /**
     * 查询采购订单列表
     * 
     * @param phgRmOrder 采购订单
     * @return 采购订单
     */
    @Override
    public List<PhgRmOrder> selectPhgRmOrderList(PhgRmOrder phgRmOrder)
    {
        return phgRmOrderMapper.selectPhgRmOrderList(phgRmOrder);
    }

    /**
     * 新增采购订单
     * 
     * @param phgRmOrder 采购订单
     * @return 结果
     */
    @Override
    public int insertPhgRmOrder(PhgRmOrder phgRmOrder)
    {
        return phgRmOrderMapper.insertPhgRmOrder(phgRmOrder);
    }

    /**
     * 修改-新增采购订单
     *
     * @param phgRmOrder 采购订单
     * @return 结果
     */
    @Override
    public AjaxResult insertRmOrder(List<PhgRmOrder> phgRmOrder)
    {
        BaseController baseController = new BaseController();
        PhgRequirement phgRequirement = new PhgRequirement();
        String username = baseController.getUsername();
        Date date = DateUtils.getNowDate();
        String purchaseOrderSn = CodeUtils.purchaseOrderCodeCreat();
        for(PhgRmOrder phgRmOrderList :phgRmOrder){
            phgRmOrderList.setCreatedBy(username);
            phgRmOrderList.setCreatedTime(date);
            phgRmOrderList.setSn(purchaseOrderSn);
            //设置到货状态为0：表示未到货
            phgRmOrderList.setArriveState(0);
        }
        //更改采购需求状态
        for(int i=0;i<phgRmOrder.size();i++){
            Long orderId = phgRmOrder.get(0).getOrderId();
            Long proId = phgRmOrder.get(i).getProId();
            phgRequirement.setState(2);
            phgRequirement.setOrderId(orderId);
            phgRequirement.setProId(proId);
            phgRequirementMapper.updatePhgRequirementState(phgRequirement);
        }
        phgRmOrderMapper.insertRmOrder(phgRmOrder);
        return AjaxResult.success("采购订单下达成功");
    }

    /**
     * 修改采购订单
     * 
     * @param phgRmOrder 采购订单
     * @return 结果
     */
    @Override
    public int updatePhgRmOrder(PhgRmOrder phgRmOrder)
    {
        return phgRmOrderMapper.updatePhgRmOrder(phgRmOrder);
    }

    /**
     * 批量删除采购订单
     * 
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deletePhgRmOrderByIds(Long[] ids)
    {
        return phgRmOrderMapper.deletePhgRmOrderByIds(ids);
    }

    /**
     * 删除采购订单信息
     * 
     * @param id 采购订单主键
     * @return 结果
     */
    @Override
    public int deletePhgRmOrderById(Long id)
    {
        return phgRmOrderMapper.deletePhgRmOrderById(id);
    }
}
